package com.wangxinrou.wangxinrou_surprisemeal.common;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import lombok.Data;

// 设置统一资源返回结果集
@Data
@Builder(toBuilder = true)
@AllArgsConstructor
@Setter
@Getter
@Slf4j
public class Result {
    @Schema(description = "返回码")
    private Integer code;

    @Schema(description = "返回消息")
    private String message;

    @Schema(description = "是否成功")
    private Boolean success;

    @Schema(description = "返回数据")
    private Object data;

    // 默认构造函数，私有化
    private Result() {}

    // 返回成功的结果集
    public static Result success() {
        return buildResult(ResultCode.SUCCESS, null);
    }

    // 返回带参的成功结果集
    public static Result success(Object data) {
        return buildResult(ResultCode.SUCCESS, data);
    }

    // 返回失败的结果集
    public static Result error(String info) {
        return buildResult(ResultCode.UNKNOWN_REASON.getCode(), info, false);
    }

    // 通用构建方法
    public static Result buildResult(ResultCode resultCodeEnum, Object data) {
        return buildResult(resultCodeEnum.getCode(), resultCodeEnum.getMessage(), resultCodeEnum.isSuccess(), data);
    }

    // 重载的构建方法
    private static Result buildResult(Integer code, String message, Boolean success) {
        return buildResult(code, message, success, null);
    }

    private static Result buildResult(Integer code, String message, Boolean success, Object data) {
        Result result = new Result();
        result.setCode(code);
        result.setMessage(message);
        result.setSuccess(success);
        result.setData(data);
        return result;
    }

    // 其他链式方法
    public Result success(Boolean success) {
        this.setSuccess(success);
        return this;
    }

    public Result message(String message) {
        this.setMessage(message);
        return this;
    }

    public Result code(Integer code) {
        this.setCode(code);
        return this;
    }

    public Result data(Object data) {
        this.setData(data);
        return this;
    }
}